import shelve

def show_info(name,age):
    print(f'name={name} age={age}')

name = ['lilisi','Jack','Tom']
info = {'name': "Jack",'age':18,'address':'beijing'}

# 持久化
with shelve.open('demo') as data:
    data['name'] = name
    data['info'] = info
    data['func'] = show_info
    
# 更新方法1: 修改后重新赋值
with shelve.open('demo') as data:
    tmp_name = data['name']
    tmp_name.append('Alex')
    data['name'] = tmp_name
    print(data['name'])     #['lilisi', 'Jack', 'Tom', 'Alex']

# 更新方法2: writeback=True
with shelve.open('demo',writeback=True) as data:
    data['name'].append('Eric')
    print(data['name'])     #['lilisi', 'Jack', 'Tom', 'Alex', 'Eric']

